﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Installer
{
    class RunInstallers
    {
        private string _Current;

        public RunInstallers()
        {
            _Current = null;
        }

        public void RunAllInstallers(List<string> ToInstall)
        {


            foreach (string Software in ToInstall)
            {

                string Section = Software.Split('#')[0];
                string Key = Software.Split('#')[1];
                string cmd = Installer.IniHandler.IniOptions.Instance.GetSectionKeyData(Section, Key);
                _Current = Key;
                RunCommand(cmd);
                System.Diagnostics.Debug.WriteLine("Command to run: " + cmd);

            }
        }

        public string GetCurrent()
        {
            return _Current;
        }

        private void RunCommand(string Command)
        {


            try
            {
                // create the ProcessStartInfo using "cmd" as the program to be run,
                // and "/c " as the parameters.
                // Incidentally, /c tells cmd that we want it to execute the command that follows,
                // and then exit.
                System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + Command);

                // The following commands are needed to redirect the standard output.
                // This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
                // Get the output into a string
                string result = proc.StandardOutput.ReadToEnd();
                // Display the command output.
                Console.WriteLine(result);
            }
            catch (Exception objException)
            {
                // Log the exception
            }
        }
    }
}

